package example;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO function description
 *
 * @author m30026548
 * @since 2025-11-18
 */
public class Exam004 {

    /**
     *
     * 【软件认证】电话拦截
     *
     * 终端产品的儿童电话手表有防骚扰特性，对电话呼入按如下规则处理：
     *
     * 如果来电号码在白名单中，则电话要接通。
     * 否则电话自动拒接。
     * 现给定一组电话呼入和白名单操作的过程记录operations，operations[i]表示的操作有以下几种（号码为举例）：
     *
     * ["C", "13300000000"] 表示一次电话号码13300000000呼入；
     * ["W", "13144444444"] 表示添加一个号码13144444444到白名单中；
     * ["W", "03712832*"] 表示以 03712832 开头的号码都为白名单（*代表通配符，且仅在结尾）。
     * 请按照给定的过程记录，分别统计每个呼入号码的接通和拒接次数：
     *
     * 每个号码的统计记录的格式如：["13144444444", 1, 3]，表示号码 13144444444 接通 1 次，拒接 3 次。
     * 统计记录的输出顺序：按照给定记录中号码首次呼入出现的先后顺序。
     * 输入
     * 一个参数operations，1 <= operations.length <= 100。
     * operations[i][0]仅为"C"或"W"；3 <= operations[i][1].length <= 11，operations[i][1] 仅包含数字和*
     *
     * 输出
     * 呼入号码的统计记录
     * 注意：需要按号码出现的顺序输出统计记录
     *
     */


    List<MyRecord> getPhoneRecords(List<Operation> operations) {

        List<String> whiteList = new ArrayList<>();
        Map<String, MyRecord> phoneRecoderMap = new LinkedHashMap<>();
        for (Operation operation : operations) {
            if (operation.opType == 'C') {
                String currentNumber = operation.number;
                MyRecord myRecord;
                if (phoneRecoderMap.get(currentNumber) == null) {
                    myRecord = new MyRecord(currentNumber);
                } else {
                    myRecord = phoneRecoderMap.get(currentNumber);
                }
                if (isInWhiteList(whiteList, currentNumber)) {
                    myRecord.accepted++;
                } else {
                    myRecord.refused++;
                }
                phoneRecoderMap.put(currentNumber, myRecord);
            } else if (operation.opType == 'W') {
                whiteList.add(operation.number);
            }
        }

        List<MyRecord> myRecordList = new ArrayList<>();
        for (String next : phoneRecoderMap.keySet()) {
            myRecordList.add(phoneRecoderMap.get(next));
        }
        return myRecordList;
    }

    private boolean isInWhiteList(List<String> whiteList, String currentNumber) {
        boolean isIn = false;
        for (String whiteSingle : whiteList) {
            if (whiteSingle.contains("*")) {
                String perfix = whiteSingle.replace("*", "");
                if (currentNumber.startsWith(perfix)) {
                    isIn = true;
                    break;
                }
            }else{
                if (currentNumber.equals(whiteSingle)) {
                    isIn = true;
                    break;
                }
            }
        }
        return isIn;
    }


    public static void main(String[] args) {
        Exam004 exam004 = new Exam004();
        List<Operation> operations = new ArrayList<>();

        operations.add(new Operation('C', "13300000000"));
        operations.add(new Operation('W', "13144444444"));
        operations.add(new Operation('C', "13144444444"));
        operations.add(new Operation('C', "03712832444"));
        operations.add(new Operation('C', "03712832233"));
        operations.add(new Operation('W', "03712832*"));
        operations.add(new Operation('C', "03712832444"));
        List<MyRecord> phoneRecords = exam004.getPhoneRecords(operations);
        for (MyRecord phoneRecord : phoneRecords) {
            System.out.printf(phoneRecord.toString());
        }

        System.out.println();
        System.out.println("-----------------------");

//        [["C", "13300000000"], ["W", "13144444445"], ["C", "13144444445"], ["C", "03712832236"], ["C", "03712832338"], ["W", "03712832*"], ["C", "10371283244"], ["C", "03712832888"]]

        List<Operation> operationsSecond = new ArrayList<>();

        operationsSecond.add(new Operation('C', "13300000000"));
        operationsSecond.add(new Operation('W', "13144444445"));
        operationsSecond.add(new Operation('C', "13144444445"));
        operationsSecond.add(new Operation('C', "03712832236"));
        operationsSecond.add(new Operation('C', "03712832338"));
        operationsSecond.add(new Operation('W', "03712832*"));
        operationsSecond.add(new Operation('C', "10371283244"));
        operationsSecond.add(new Operation('C', "03712832888"));
        List<MyRecord> phoneRecordSecond = exam004.getPhoneRecords(operationsSecond);
        for (MyRecord phoneRecord : phoneRecordSecond) {
            System.out.printf(phoneRecord.toString());
        }

    }
}
